package cn.z2huo.algorithm.binarytree;

import java.util.LinkedList;
import java.util.Queue;

public class Traversal_Hierarchical {
    public void traverse(TreeNode root){
        Queue<TreeNode> queue = new LinkedList();
        if(root!=null){
            queue.offer(root);
        }
        while(!queue.isEmpty()){
            System.out.print(queue.peek().toString()+" ");
            TreeNode node = queue.poll();
            if(node.getLeft()!=null){
                queue.offer(node.getLeft());
            }
            if(node.getRight()!=null){
                queue.offer(node.getRight());
            }
        }
        System.out.println();
    }
}
